<?php
	namespace Admin\Service;
	use Think\Service\BasicService;

	class OrderService extends BasicService{
		public function insert($model){
			return $this->db->add($model);
		}

		public function remove($condition){
			return $this->db->where($condition)->delete();
		}

		public function update($model){
			return $this->db->save($model);
		}

		public function find($condition){
			return $this->db->find($condition);
		}

		public function query($condition){
			return $this->db->where($condition)->select();
		}

		public function countNoAllocate($TEAM){
			$sql="select count(o.UNID) as count from t_order as o
					left join t_task_fin as tf on o.UNID=tf.OID
					where o.state=2 and tf.TEAM='$TEAM' ";
			$data = $this->db->query($sql);
			return $data[0][count];
		}

		public function queryFields($condition,$data,$limit){
			return $this->db->field($data)->where($condition)->limit($limit['start'],$limit['end'])->select();
		}

		public function queryCount($condition){
			return $this->db->where($condition)->count();
		}

		public function queryLimit($condition,$limit){
			$sql="select o.*,u.NICKNAME,tf.TEAM,team.`NAME` as TNAME,team.PHONE as TPHONE from t_order as o
				left join t_user as u on o.OPENID=u.OPENID
				left join t_task_fin as tf on tf.OID=o.UNID
				left join t_team as team on team.UNID=tf.TEAM
				where o.UNID like '%".$condition."%' group by o.UNID ";
			if($limit){
				$sql.="limit {$limit['start']},{$limit['end']}";
			}
			return $this->db->query($sql);
		}

		public function queryLimitCount($condition){
			$sql="select count(o.UNID) as count from t_order as o
				left join t_user as u on o.OPENID=u.OPENID
				left join t_task_fin as tf on tf.OID=o.UNID
				left join t_team as team on team.UNID=tf.TEAM
				where o.UNID like '%".$condition."%'";
			$data = $this->db->query($sql);
			return $data[0][count];
		}

		public function queryOwnOrder($condition,$limit,$team,$history){
			$sql="select o.*,u.NICKNAME,u1.NAME as DRIVER,u2.NAME as SERVICE,u1.PHONE as DPHONE,u2.PHONE AS CPHONE,u1.WXNUM as DWXNUM,u2.WXNUM AS CWXNUM,tf.DRIVER AS DID,tf.SERVICE AS CID from t_order as o
				left join t_user as u on o.OPENID=u.OPENID
				left join t_task_fin as tf on o.UNID=tf.OID
				left join t_user as u1 on tf.DRIVER=u1.UNID
				left join t_user as u2 on tf.SERVICE=u2.UNID
				where o.UNID like '%".$condition."%' and tf.TEAM='$team' ";
			if($history==0){
				$sql.=' and o.PDATE >= CURDATE() ';
			}
			$sql.=' group by o.UNID order by o.PDATE desc ';
			if($limit){
				$sql.="limit {$limit['start']},{$limit['end']}";
			}
			return $this->db->query($sql);
		}

		public function queryOwnOrderDay($condition,$limit,$team,$history){
			$sql="select o.PDATE from t_order as o
				left join t_task_fin as tf on o.UNID=tf.OID
				where o.UNID like '%".$condition."%' and tf.TEAM='$team' ";
			if($history==0){
				$sql.=' and o.PDATE >= CURDATE() ';
			}
			$sql.=' group by o.PDATE order by o.PDATE desc ';
			if($limit){
				$sql.="limit {$limit['start']},{$limit['end']}";
			}
			return $this->db->query($sql);
		}

		public function queryExportOrder($condition,$limit,$team){
			$sql="select o.* from t_order as o
				left join t_task_fin as tf on o.UNID=tf.OID
				where tf.TEAM='$team' and o.PDATE IN ( ";
			foreach ($condition as $key => $value) {
				$sql.="'$value'"." , ";
			}
			$sql = rtrim( trim($sql), ',' );
			
			$sql.=") group by o.UNID order by o.PDATE ";

			if($limit){
				$sql.="limit {$limit['start']},{$limit['end']}";
			}
			return $this->db->query($sql);
		}

		public function findOrder($condition){
			$sql="select o.*,u.NICKNAME,u1.NAME as DRIVER,u2.NAME as SERVICE,u1.PHONE as DPHONE,u2.PHONE AS CPHONE,u1.WXNUM as DWXNUM,u2.WXNUM AS CWXNUM,tf.DRIVER AS DID,tf.SERVICE AS CID from t_order as o
				left join t_user as u on o.OPENID=u.OPENID
				left join t_task_fin as tf on o.UNID=tf.OID
				left join t_user as u1 on tf.DRIVER=u1.UNID
				left join t_user as u2 on tf.SERVICE=u2.UNID
				left join t_task as t on o.UNID=t.OID
				where o.UNID = '".$condition."' group by o.UNID ";
			return $this->db->query($sql);
		}

		public function queryAllocateLimit($condition,$limit,$team,$history,$STATE,$sort,$sorttype){
			$sql="select o.*,u.NICKNAME,u1.NAME as DRIVER,u1.OPENID as DOPENID,u2.NAME as SERVICE,u1.PHONE as DPHONE,u2.PHONE AS CPHONE,u1.WXNUM as DWXNUM,u2.WXNUM AS CWXNUM,tf.DRIVER AS DID,tf.SERVICE AS CID,tf.TEAM,dc.FEE from t_order as o
				left join t_user as u on o.OPENID=u.OPENID
				left join t_task_fin as tf on o.UNID=tf.OID
				left join t_user as u1 on tf.DRIVER=u1.UNID
				left join t_user as u2 on tf.SERVICE=u2.UNID
				left join t_task as t on o.UNID=t.OID
				left join t_deploycost as dc on dc.OID=o.UNID
				where o.UNID like '%".$condition."%' and (tf.TEAM='$team' or t.`OWNER`='$team' or dc.FORMERTEAM='$team') ";
			if(!$condition){
				if($history==0){
					$sql.=' and o.PDATE >= CURDATE() ';
				}
			}
			if(!empty($STATE)){
				if($STATE==2){
					$sql.=" and (o.STATE = '$STATE' or tf.DRIVER is null )";
				}else{
					$sql.=" and o.STATE = '$STATE' ";
				}
			}
			$sql.=' group by o.UNID ';
			if($sort=='CREATETIME'){
				$sql.=' order by o.CREATETIME '.$sorttype;
			}else{
				$sql.=' order by o.PDATE '.$sorttype;
			}
			
			if($limit){
				$sql.=" limit {$limit['start']},{$limit['end']}";
			}
			return $this->db->query($sql);
		}

		public function queryAllocateLimitCount($condition,$team,$history,$STATE){
			$sql="select count(o.UNID) as count from t_order as o
				left join t_user as u on o.OPENID=u.OPENID
				left join t_task_fin as tf on o.UNID=tf.OID
				left join t_task as t on o.UNID=t.OID
				left join t_deploycost as dc on dc.OID=o.UNID
				where o.UNID like '%".$condition."%' and (tf.TEAM='$team' or t.`OWNER`='$team' or dc.FORMERTEAM='$team') ";
			if(!$condition){
				if($history==0){
					$sql.=' and o.PDATE >= CURDATE() ';
				}
			}
			if(!empty($STATE)){
				if($STATE==2){
					$sql.=" and (o.STATE = '$STATE' or tf.DRIVER is null )";
				}else{
					$sql.=" and o.STATE = '$STATE' ";
				}
			}
			$data = $this->db->query($sql);
			return $data[0][count];
		}

		/**
		 * 分配订单给司机和客服
		 * @Author   :ZengYi
		 * @DateTime :2016-12-09T14:43:04+0800
		 * @Detail   :
		 * @param    [type]                    $UNID    [订单ID]
		 * @param    [type]                    $STATE   [状态]
		 * @param    [type]                    $DRIVER  [司机ID]
		 * @param    [type]                    $SERVICE [客服ID]
		 * @return   [type]                             [description]
		 */
		public function allocateOrder($UNID,$STATE,$DRIVER,$SERVICE,$MID){
			$this->db->startTrans();
			$sql1="UPDATE t_order set STATE='$STATE' WHERE UNID='$UNID' ";
			if($this->db->execute($sql1)){
				$sql2="UPDATE t_task_fin set DRIVER='$DRIVER',SERVICE='$SERVICE',MANAGER='$MID' WHERE OID='$UNID' ";
				if($this->db->execute($sql2)){
					$this->db->commit();
					return true;
				}
			}
			return false;
		}

		public function allocateDriver($UNID,$DRIVER,$MID){
			$sql="UPDATE t_task_fin set DRIVER='$DRIVER',MANAGER='$MID' WHERE OID='$UNID' ";
			if($this->db->execute($sql)){
				return true;
			}
			return false;
		}

		public function allocateService($UNID,$SERVICE,$MID){
			$sql="UPDATE t_task_fin set SERVICE='$SERVICE',MANAGER='$MID' WHERE OID='$UNID' ";
			if($this->db->execute($sql)){
				return true;
			}
			return false;
		}

		public function Add_task($save){
			return $this->db->add($save);
		}

		public function queryAdminOpenidByOid($OID){
			$sql="select u.OPENID from t_task as t 
					left join t_team as te on t.`OWNER`=te.UNID
					left join t_user as u on te.UNID=u.TEAM 
					left join t_login as l on u.UNID=L.UNID
					where l.ROLEID='583e7d10B01' and OID='$OID'";
			return $this->db->query($sql);
		}
	}

	?>